package com.zh.kotlin_mvvm.utils;

import android.annotation.SuppressLint;
import android.content.Context;
import android.content.res.Resources;
import android.graphics.Color;
import android.graphics.drawable.ColorDrawable;
import android.util.AttributeSet;
import android.view.View;
import android.widget.EditText;
import android.widget.NumberPicker;

import com.zh.kotlin_mvvm.R;

import java.lang.reflect.Field;

public class QNumberPicker extends NumberPicker {

    public QNumberPicker(Context context) {
        super(context);
    }

    public QNumberPicker(Context context, AttributeSet attrs) {
        super(context, attrs);
    }

    public QNumberPicker(Context context, AttributeSet attrs, int defStyleAttr) {
        super(context, attrs, defStyleAttr);
    }

    @Override
    public void addView(View child) {
        super.addView(child);
        updateView(child);
    }

    @Override
    public void addView(View child, int index,
                        android.view.ViewGroup.LayoutParams params) {
        super.addView(child, index, params);
        updateView(child);
    }

    @Override
    public void addView(View child, android.view.ViewGroup.LayoutParams params) {
        super.addView(child, params);
        updateView(child);
    }

    public void updateView(View view) {
        setDescendantFocusability(NumberPicker.FOCUS_BLOCK_DESCENDANTS);
        if (view instanceof EditText) {
            //这里修改字体的属性
            ((EditText) view).setTextColor(Color.parseColor("#BAA785"));
        }
    }

    @Override
    public void setDisplayedValues(String[] displayedValues) {
        super.setDisplayedValues(displayedValues);
//        setNumberPickerDividerColor(this);
    }

    @SuppressLint("SoonBlockedPrivateApi")
    public void setNumberPickerDividerColor(NumberPicker numberPicker) {
        try {
            Field pickerField = numberPicker.getClass().getDeclaredField("mSelectionDivider");
            pickerField.setAccessible(true);
            //设置分割线的颜色值
            pickerField.set(numberPicker, new ColorDrawable(this.getResources().getColor(R.color.colorAccent)));
            numberPicker.postInvalidate();
        } catch (IllegalArgumentException | Resources.NotFoundException | IllegalAccessException | NoSuchFieldException e) {
            e.printStackTrace();
        }
    }
}
